<?php
	/**
	* 文章系统类
	*
	*/
	class Article{
		//参数设置
		public $savePath            =   '';
		public $saveDir             =   ''; //保存文章的文件目录
		private $unsetContent       =   ''; //
		private $artNewName         =   ''; //文章的名字
		private $artPath            =   ''; //文章的路径
		private $ext                =   '.html'; //默认文件后缀名
		
		private $artSaveField = array(
			'file_url'        => '',
			'ctime'=> 0,
		);		
		//错误提示心信息
		private $errorMsg = array(
			'result'     => false,
			'msg'        => 'failed'
		);
		
		function __construct(){
			$this->saveDir = './Public/Upload/article/';
		}
		
		function Article(){
			$this->saveDir = './Public/Upload/article/';
		}
		
		/**
		* 保存文件
		* 
		*/
		function save($datas){			
			/*if(!isset($datas['content'])) {
				$this->setErrorMsg(false,'不存在content字段');
				return;
			}*/
			if(!array_key_exists('content', $datas)) {
				$this->setErrorMsg(false,'不存在content字段');
				return;
			}
			
			//determine whether the field exists
			if(!isset($datas['ctime'])){
				$datas['ctime'] = $this->getUnixTime(); 
			}
			
			//get html code and escape
			$content             =   stripslashes($datas['content']);
			unset($datas['content']);
			
			//create relative dir of time
			$ymd                 =   date("Ymd");		
			$this->saveDir      .=   $ymd . "/";  //absolute path
			
			if(!file_exists($this->saveDir)){
				mkdir($this->saveDir);
			}			
			//name			
			$art_new_name        =   $this->name();
			$art_savePath        =   $this->saveDir . $art_new_name . '.html';
			$this->savePath      =   $ymd . "/" . $art_new_name . $this->ext;
			//echo $art_savePath . '</br>';
			//write into file						
			$fp                  =   fopen($art_savePath, 'w'); 
			fwrite($fp, $content);	
			fclose($fp);			
			//save successfully and save the params
			//$this->artSaveField ['file_url']        =   $this->savePath;
			$this->artSaveField ['src']            	=   $this->savePath;
			$this->artSaveField ['url']            	=   $this->savePath;
			$this->artSaveField ['ctime']    		=   $this->getUnixTime();
			
			return array_merge($datas ,$this->artSaveField);
		}
		
		/**
		 * Function : update files in dir and update the datas in the database.
		 *
		 */
		function updateArticles($datas){
			if(!isset($datas['ctime'])){
				$datas['ctime'] = $this->getUnixTime(); 
			}
			
			//get html code and escape
			$content             =   stripslashes($datas['content']);
			unset($datas['content']);
			//create relative dir of time
			$ymd                 =   date("Ymd");		
			$this->saveDir      .=   $ymd . "/";  //absolute path
			
			if(!file_exists($this->saveDir)){
				mkdir($this->saveDir);
			}
			//name			
			$art_new_name        =   $this->name();
			$art_savePath        =   $this->saveDir . $art_new_name . '.html';
			$this->savePath      =   $ymd . "/" . $art_new_name . $this->ext;
			//echo $art_savePath . '</br>';
			//write into file						
			$fp                  =   fopen($art_savePath, 'w'); 
			fwrite($fp, $content);	
			fclose($fp);			
			//save successfully and save the params
			//$this->artSaveField ['file_url']        =   $this->savePath;
			$this->artSaveField ['src']            	=   $this->savePath;
			$this->artSaveField ['url']            	=   $this->savePath;
			$this->artSaveField ['ctime']    		=   $this->getUnixTime();
			
			return array_merge($datas ,$this->artSaveField);
		}
		
		/**
		* 存储文件并且存入数据库
		* 
		*/
		function saveToTable($datas, $table = ''){
			//add code here
		}
		
		/**
		* 命名
		* @return: $art_name
		*/
		function name(){
			return date("YmdHis") . rand(1000,9999);
		}
		
		/**
		* 判断文件是否存在
		*/
		function isExist($file){
			if(file_exists($file)) return true;
			else  return false;
		}
		
		/**
		* 设置错误信息
		* @param: $result 
		* @param: $msg
		*/
		function setErrorMsg($result = false, $msg = '失败'){
			$this->errorMsg ['result'] = $result;
			$this->errorMsg ['msg'] = $msg;
		}
		
		/**
		* 获取错误信息
		* @param: array('result'=>false,'msg'=>'失败')
		*/
		function getErrorMsg(){
			return $this->errorMsg;
		}
		
		/**
		* 获取当前Unix时间戳
		*/
		function getUnixTime(){
			return time();
		}
		/**
		 * 设置保存文件的后缀名
		 * @param string $ext
		 */
		function setExt($ext = '.html'){
			$this->ext = $ext;
		}
		
		/**
		 * 获取保存文件的后缀名
		 * @return string
		 */
		function getExt(){
			return $this->ext;
		}
		
		/**
		 * 获取htmlwen文件内容
		 * @param string $file
		 * @return void|string|NULL
		 */
		static function  getFileContent($file = ""){
			$dir           =   './Public/Upload/article/';
			if($file == ""){
				return null;
			}
			//如果他给了全路径
			if(file_exists($file)){
				return file_get_contents($file);
			}
						
 			$filePath       =   $dir . $file;
			if(file_exists($filePath)){
				return file_get_contents($filePath);
			}
			else 
				return null;
		}
		
		
	}


?>